package com.lottery.ruok.lottrey.fragment;

import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.ListView;

import com.lottery.ruok.lottrey.R;
import com.lottery.ruok.lottrey.adapter.ChildPrizeAdapter;
import com.lottery.ruok.lottrey.eventbusObject.PrizePushInfo;
import com.lottery.ruok.lottrey.retrofitModule.response.GetWholeCategoryResponse;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import count_down.CountDownManager;

import static com.lottery.ruok.lottrey.R.id.listview;

/**
 * Created by ASUS on 2017/7/27.
 */

public class ChildPrizeFragment extends BaseFragment {

    @BindView(listview)
    ListView listView;

    private ChildPrizeAdapter myAdapter;
    private GetWholeCategoryResponse data;
    private View contentView;


    public static ChildPrizeFragment newInstance(){
        return new ChildPrizeFragment();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        if (contentView != null) {
            ViewGroup parent = (ViewGroup) contentView.getParent();
            if (parent != null) {
                parent.removeView(contentView);
            }
        }else{
            contentView = inflater.inflate(R.layout.prize_lottery_fragment,
                    container, false);
        }
        ButterKnife.bind(this, contentView);
        init();
        return contentView;
    }

    public void setDatas(GetWholeCategoryResponse g) {
        data = g;
        if (myAdapter != null) {
            myAdapter.setList(g.getItems());
        }
    }

    private void init() {
        if (myAdapter == null) {
            myAdapter = new ChildPrizeAdapter(getActivity());
        }
        if (data != null) {
            myAdapter.setList(data.getItems());
        }
        listView.setAdapter(myAdapter);

        listView.setOnScrollListener(new AbsListView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(AbsListView view, int scrollState) {
                setSwipState();
            }

            @Override
            public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {

            }
        });
    }

    public void setSwipState() {
        if (listView != null && listView.getChildAt(0) != null) {
            ((PrizeMainFragment) getParentFragment()).swipeRefreshLayout.setEnabled(listView.getFirstVisiblePosition() == 0 && listView.getChildAt(0).getTop() == 0);
        }
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        EventBus.getDefault().register(this);
    }

    @Override
    public void onDetach() {
        EventBus.getDefault().unregister(this);
        super.onDetach();
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(PrizePushInfo event) {
        if (event.getCode() != null) {
            if(data == null ){
                return;
            }
            List<GetWholeCategoryResponse.ItemsBean> items = data.getItems();
            for(GetWholeCategoryResponse.ItemsBean item : items){
                if(item.getCode().equals(event.getCode())){
                    item.setPrize_num(event.getPrizeNum());
                    item.setNo(event.getIssueNo());
                    item.setTime(item.getInterval());
                    myAdapter.notifyDataSetChanged();
                }
            }
        }
    }
}